<template>
	<view class="after-sale-apply-container page-container">
		<!-- 订单信息 -->
		<view class="order-info-card">
			<view class="card-title">订单信息</view>
			<view class="order-detail">
				<text class="order-no">订单号：{{ orderInfo.order_no }}</text>
				<text class="order-date">下单时间：{{ orderInfo.create_time }}</text>
			</view>
			<view class="product-list">
				<view class="product-item" v-for="(product, index) in orderInfo.products" :key="index">
					<image :src="$tools.buildUrl(product.main_image)" mode="aspectFill" class="product-image"></image>
					<view class="product-info">
						<text class="product-name">{{ product.spu_name }}</text>
						<text class="product-spec">{{ product.sku_name }}</text>
						<text class="product-price">¥{{ product.actual_price }} x{{ product.quantity }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 售后类型选择 -->
		<view class="form-section">
			<view class="section-title">售后类型</view>
			<view class="type-options">
				<view class="type-option" v-for="(type, index) in afterSaleTypes" :key="index"
					:class="{ active: selectedType === index }" @click="selectType(index)">
					<view class="option-icon">{{ type.icon }}</view>
					<view class="option-info">
						<text class="option-name">{{ type.name }}</text>
						<text class="option-desc">{{ type.description }}</text>
					</view>
					<view class="option-radio" :class="{ checked: selectedType === index }"></view>
				</view>
			</view>
		</view>

		<!-- 售后原因 -->
		<view class="form-section">
			<view class="section-title">售后原因</view>
			<view class="reason-options">
				<view class="reason-item" v-for="(reason, index) in reasonOptions" :key="index"
					:class="{ active: selectedReason === index }" @click="selectReason(index)">
					<text class="reason-text">{{ reason }}</text>
					<view class="reason-radio" :class="{ checked: selectedReason === index }"></view>
				</view>
			</view>
		</view>

		<!-- 问题描述 -->
		<view class="form-section">
			<view class="section-title">问题描述</view>
			<textarea class="description-input" v-model="description" placeholder="请详细描述您遇到的问题，以便我们更好地为您处理"
				maxlength="500"></textarea>
			<text class="char-count">{{ description.length }}/500</text>
		</view>

		<!-- 上传图片 -->
		<view class="form-section">
			<view class="section-title">上传图片（可选）</view>
			<view class="upload-section">
				<view class="image-list">
					<view class="image-item" v-for="(image, index) in uploadedImages" :key="index">
						<image :src="$tools.buildUrl(image)" mode="aspectFill" class="uploaded-image"></image>
						<view class="delete-btn" @click="deleteImage(index)">×</view>
					</view>
					<view class="upload-btn" v-if="uploadedImages.length < 6" @click="uploadImage">
						<text class="upload-icon">+</text>
						<text class="upload-text">上传图片</text>
					</view>
				</view>
				<text class="upload-tip">最多可上传6张图片，支持jpg、png格式</text>
			</view>
		</view>

		<!-- 联系方式 -->
		<view class="form-section">
			<view class="section-title">联系人</view>
			<view class="contact-input">
				<input class="contact-field" v-model="contactName" placeholder="请输入您的姓名" type="text" maxlength="25" />
			</view>
		</view>
		<view class="form-section">
			<view class="section-title">联系方式</view>
			<view class="contact-input">
				<input class="contact-field" v-model="contactPhone" placeholder="请输入您的手机号码" type="number"
					maxlength="11" />
			</view>
		</view>

		<!-- 售后政策 -->
		<view class="policy-section">
			<view class="section-title">
				<text class="title-text">售后政策</text>
			</view>
			<view class="policy-content">
				<view class="policy-item" v-for="(item, index) in policies" :key="index">
					<view class="policy-icon">
						<u-icon :name="item.icon" size="24" color="#1c705e"></u-icon>
					</view>
					<view class="policy-info">
						<text class="policy-title">{{ item.title }}</text>
						<text class="policy-desc">{{ item.desc }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 提交按钮 -->
		<view class="submit-section">
			<button class="submit-btn" @click="submitAfterSale" :disabled="!canSubmit">
				提交申请
			</button>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				orderId: '',
				orderInfo: {},
				selectedType: 0,
				selectedReason: -1,
				description: '',
				uploadedImages: [],
				contactName: '',
				contactPhone: '',
				afterSaleTypes: [{
						id: 1,
						name: '退货退款',
						description: '商品有质量问题或不符合描述',
						icon: '📦'
					},
					{
						id: 2,
						name: '换货',
						description: '商品有质量问题需要更换',
						icon: '🔄'
					},
					{
						id: 3,
						name: '仅退款',
						description: '未收到商品或商品丢失',
						icon: '💰'
					}
				],
				reasonOptions: [
					'商品质量问题',
					'商品与描述不符',
					'商品破损',
					'收到商品不完整',
					'不喜欢/不想要了',
					'其他原因'
				],
				// 售后政策
				policies: [{
						icon: 'clock',
						title: '7天无理由退换',
						desc: '商品签收7天内，无质量问题可申请退换货'
					},
					{
						icon: 'shield',
						title: '质量保证',
						desc: '商品质量问题，支持退换货'
					},
					{
						icon: 'checkmark-circle',
						title: '快速处理',
						desc: '售后申请提交后，1-3个工作日内处理'
					}
				]
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo']),

			canSubmit() {
				return this.selectedType !== -1 &&
					this.selectedReason !== -1 &&
					this.description.trim() !== '' &&
					this.contactName.trim() !== '' &&
					this.contactPhone.length === 11
			}
		},
		onLoad(options) {
			if (options.orderId) {
				this.orderId = options.orderId
				this.contactName = !this.userInfo.realname.trim() ? this.userInfo.nickname : this.userInfo.realname
				this.contactPhone = this.$secret.decryptAES(this.userInfo.mobile)
				this.loadOrderInfo()
			}
		},
		methods: {
			// 加载订单信息
			loadOrderInfo() {
				this.$api.order.GetOrdersInfo({
					id: this.orderId
				}).then(res => {
					this.orderInfo = res.data;
				})
			},

			// 选择售后类型
			selectType(index) {
				this.selectedType = index
			},

			// 选择售后原因
			selectReason(index) {
				this.selectedReason = index
			},

			// 上传图片
			uploadImage() {
				uni.chooseImage({
					count: 6 - this.uploadedImages.length,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						res.tempFilePaths.forEach(tempFilePath => {
							this.uploadPhoto(tempFilePath);
						})
					}
				})
			},

			// 上传图片到服务器
			uploadPhoto(imageUrl) {
				uni.showLoading({
					title: '正在上传'
				})
				const uploadUrl = '/api/App_Upload/UploadFile';
				const headerConfig = this.$sign.buildHeaderSign(uploadUrl, this.userInfo, {}, 'POST'); // 生成签名
				//上传到服务器
				uni.uploadFile({
					url: this.$tools.baseUrl() + uploadUrl,
					filePath: imageUrl, //要上传文件资源的路径 (本地路径)
					name: 'photo', //文件对应的 key，开发者在服务端可以通过这个 key 获取文件的二进制内容
					header: headerConfig,
					formData: {}, //HTTP 请求中其他额外的 form data       
					success: res => {
						uni.hideLoading()
						var jsonObj = this.$tools.parseJsonObject(res.data)
						if (jsonObj.code == 100) {
							//保存成功 
							this.uploadedImages.push(jsonObj.data.relativeUrl)
						} else {
							//保存失败							
							uni.showToast({
								title: jsonObj.msg,
								icon: 'error'
							})
						}
					},
					fail: err => {
						uni.hideLoading()
						uni.showToast({
							title: '上传图片错误：' + JSON.stringify(err),
							icon: 'error'
						})
					}
				})
			},

			// 删除图片
			deleteImage(index) {
				this.uploadedImages.splice(index, 1)
			},

			// 提交售后申请
			submitAfterSale() {
				if (!this.canSubmit) {
					uni.showToast({
						title: '请完善申请信息',
						icon: 'none'
					})
					return
				}

				const afterSaleData = {
					order_id: this.orderId,
					after_sale_type: this.afterSaleTypes[this.selectedType].id, // 售后类型：1-退货 2-换货 3-仅退款 4-补寄
					reason: this.reasonOptions[this.selectedReason], // 申请原因
					reason_remark: this.description, // 申请描述
					evidence: this.uploadedImages, // 举证材料（图片/视频URL数组）
					contactName: this.contactName, // 联系人
					contactPhone: this.contactPhone, // 联系方式					
					...this.orderInfo
				}

				uni.showLoading({
					title: '提交中...'
				})

				this.$api.order.AfterSalesApply(afterSaleData).then(res => {
					uni.hideLoading()
					uni.showModal({
						title: '申请成功',
						content: '您的售后申请已提交，我们会在24小时内处理',
						showCancel: false,
						success: () => {
							uni.redirectTo({
								url: '/pages/after-sale/list'
							})
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.after-sale-apply-container {
		background-color: #f8f9fa;
	}

	/* 订单信息卡片 */
	.order-info-card {
		background: white;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.card-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}

	.order-detail {
		margin-bottom: 20rpx;
	}

	.order-no,
	.order-date {
		display: block;
		font-size: 26rpx;
		color: #666;
		margin-bottom: 8rpx;
	}

	.product-list {
		border-top: 1rpx solid #f0f0f0;
		padding-top: 20rpx;
	}

	.product-item {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.product-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 12rpx;
		margin-right: 20rpx;
	}

	.product-info {
		flex: 1;
	}

	.product-name {
		font-size: 28rpx;
		color: #333;
		display: block;
		margin-bottom: 8rpx;
	}

	.product-spec {
		font-size: 24rpx;
		color: #999;
		display: block;
		margin-bottom: 8rpx;
	}

	.product-price {
		font-size: 26rpx;
		color: #52a58a;
		font-weight: bold;
	}

	/* 表单区域 */
	.form-section {
		background: white;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
	}

	/* 售后类型选择 */
	.type-options {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}

	.type-option {
		display: flex;
		align-items: center;
		padding: 30rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 16rpx;
		transition: all 0.3s ease;
	}

	.type-option.active {
		border-color: #52a58a;
		background-color: #f0f8f5;
	}

	.option-icon {
		font-size: 40rpx;
		margin-right: 20rpx;
	}

	.option-info {
		flex: 1;
	}

	.option-name {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
		display: block;
		margin-bottom: 8rpx;
	}

	.option-desc {
		font-size: 24rpx;
		color: #666;
	}

	.option-radio {
		width: 40rpx;
		height: 40rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 50%;
		position: relative;
	}

	.option-radio.checked {
		border-color: #52a58a;
	}

	.option-radio.checked::after {
		content: '';
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 20rpx;
		height: 20rpx;
		background-color: #52a58a;
		border-radius: 50%;
	}

	/* 售后原因 */
	.reason-options {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
	}

	.reason-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.reason-item:last-child {
		border-bottom: none;
	}

	.reason-item.active .reason-text {
		color: #52a58a;
		font-weight: bold;
	}

	.reason-text {
		font-size: 28rpx;
		color: #333;
	}

	.reason-radio {
		width: 36rpx;
		height: 36rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 50%;
		position: relative;
	}

	.reason-radio.checked {
		border-color: #52a58a;
	}

	.reason-radio.checked::after {
		content: '';
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 16rpx;
		height: 16rpx;
		background-color: #52a58a;
		border-radius: 50%;
	}

	/* 问题描述 */
	.description-input {
		width: 100%;
		min-height: 200rpx;
		padding: 20rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 12rpx;
		font-size: 28rpx;
		color: #333;
		line-height: 1.5;
		box-sizing: border-box;
	}

	.char-count {
		display: block;
		text-align: right;
		font-size: 24rpx;
		color: #999;
		margin-top: 12rpx;
	}

	/* 图片上传 */
	.upload-section {}

	.image-list {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
		margin-bottom: 20rpx;
	}

	.image-item {
		position: relative;
		width: 160rpx;
		height: 160rpx;
	}

	.uploaded-image {
		width: 100%;
		height: 100%;
		border-radius: 12rpx;
		object-fit: cover;
	}

	.delete-btn {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: #ff4757;
		color: white;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		font-weight: bold;
	}

	.upload-btn {
		width: 160rpx;
		height: 160rpx;
		border: 2rpx dashed #e5e5e5;
		border-radius: 12rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #999;
	}

	.upload-icon {
		font-size: 48rpx;
		margin-bottom: 8rpx;
	}

	.upload-text {
		font-size: 24rpx;
	}

	.upload-tip {
		font-size: 24rpx;
		color: #999;
	}

	/* 联系方式 */
	.contact-input {}

	.contact-field {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		border: 2rpx solid #e5e5e5;
		border-radius: 12rpx;
		font-size: 28rpx;
		color: #333;
		box-sizing: border-box;
	}

	/* 提交按钮 */
	.submit-section {
		padding: 30rpx;
	}

	.submit-btn {
		width: 100%;
		height: 88rpx;
		background: linear-gradient(135deg, #52a58a 0%, #6bb6a3 100%);
		color: white;
		border: none;
		border-radius: 44rpx;
		font-size: 32rpx;
		font-weight: bold;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.submit-btn:disabled {
		background: #ccc;
		color: #999;
	}

	/* 售后政策 */
	.policy-section {
		background-color: #ffffff;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 40rpx;
	}


	.policy-content {
		margin-top: 20rpx;
	}

	.policy-item {
		display: flex;
		align-items: flex-start;
		margin-bottom: 30rpx;
	}

	.policy-item:last-child {
		margin-bottom: 0;
	}

	.policy-icon {
		margin-right: 20rpx;
		margin-top: 8rpx;
	}

	.policy-info {
		flex: 1;
	}

	.policy-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 8rpx;
		display: block;
	}

	.policy-desc {
		font-size: 26rpx;
		color: #666;
		line-height: 1.5;
		display: block;
	}
</style>